<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加会员</title>
    <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0.maximum-scale=1.0,user-scalable=no" name="viewport">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/bossIndex.css">
    <link rel="stylesheet" href="css/flexible.css">
    <link rel="stylesheet" href="css/mobiscroll.custom-3.0.0-beta6.min.css"/>
    <link href="css/vant.css" rel="stylesheet" type="text/css" />
    <script src="js/vue/vue.js"></script>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/fontsize.js"></script>
    <script src="js/vue/vant.js"></script>
    <script src="js/glabe.js"></script>
    <script src="js/vue/axios.js"></script>
    <script src="js/jquery.md5.js"></script>
    <script src="js/vue/qs.js"></script>
    <style>
        .hintMessage{
            width: 68% !important;
            text-align: right;
        }
        input.hint::-webkit-input-placeholder { /* WebKit browsers */ 
            color: red; 
        } 

        input.hint:-moz-placeholder { /* Mozilla Firefox 4 to 18 */ 
            color: red; 
        } 

        input.hint::-moz-placeholder { /* Mozilla Firefox 19+ */ 
            color: red; 
        } 

        input.hint:-ms-input-placeholder { /* Internet Explorer 10+ */ 
            color: red; 
        } 
        .demo{
            display: flex;
            font-size: 0.14rem;
        }
        .rightbackground img {
           margin: 0 0 0 0.04rem ;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- <div class="demo">
            <img src="images/left_icon.png"/>
            <a href="javascript:history.go(-1)">返回</a>
        </div> -->
        <div class="container_box">
            <div class="Information_entry">
                <label class="lf">姓名</label>
                <div class="rf message padding14">
                    <input placeholder="请输入会员姓名" v-model="name">
                </div>
            </div>
            <div class="Information_entry">
                <label class="lf">电话</label>
                <div class="rf message padding14 hintMessage">
                    <input v-if="id == null" type="text" oninput = "value=value.replace(/[^\d]/g,'')" maxlength="11" placeholder="作为手机端登录账号，请谨慎添加" v-model="Phone">
                    <span v-else>{{Phone}}</span>
                </div>
            </div>
            <div class="Information_entry">
                <label class="lf">性别</label>
                <div class="rf Sex padding14">
                    <label>
                        <input type=radio name=x1 value="1" :checked="Sex == 1 ? 'checked' : '' " @click="getRadioVal"><span>男</span>
                    </label>
                    <label>
                        <input type=radio name=x1 value="0" :checked="Sex == 0 ? 'checked' : '' " @click="getRadioVal"><span>女</span>
                    </label>
                </div>
            </div>
            <div class="Information_entry">
                <label class="lf">出生日期</label>
                <div class="rf message padding14"> 
                    <!-- <input v-model="date" type="text" id="age" placeholder="请选择出生日期" @click="showDate()" > -->
                    <span @click="showDate" style="font-size: 0.14rem;">{{date ? date : "请选择出生日期"}}</span>
                </div>
                <van-popup 
                v-model="dateShow"
                position="bottom"
                >
                    <van-datetime-picker
                    v-model="currentDate"
                    type="date"
                    :min-date="minDate" 
                    @confirm="dateSelect"
                    @cancel="dateNo"
                    />
                </van-popup>
            </div>   
            <div class="Information_entry">
                <label class="lf">来源</label>
                <div class="rf message padding14 rightbackground" @click="showSource()">
                    <span style="font-size: 0.14rem;">{{source ? source.Fromname : '请选择来源'}}</span>
                    <img src="./images/right_icon.png" alt="">
                </div>
                <van-popup 
                v-model="sourceShow"
                position="bottom"
                >
                    <van-picker show-toolbar :columns="sourceData" value-key="Fromname" @confirm="sourceSelect" @cancel="sourceNo"/>
                </van-popup>
            </div>
            <div class="Information_entry">
                <label class="lf">会籍顾问</label>
                <div class="rf message padding14 rightbackground" @click="adviserShow = true">
                    <span style="font-size: 0.14rem;">{{adviser ? adviser.Sname : '选择会籍顾问'}}</span>
                    <img src="./images/right_icon.png" alt="">
                </div>
                <van-popup 
                v-model="adviserShow"
                position="bottom"
                >
                    <van-picker show-toolbar :columns="stafflist" value-key="Sname" @confirm="adviserSelect" @cancel="adviserNo"/>
                </van-popup>
            </div>
            <div class="Information_entry">
                <label class="lf">教练</label>
                <div class="rf message padding14 rightbackground" @click="CoachShow = true">
                    <span style="font-size: 0.14rem;">{{Coach ? Coach.Sname : '选择教练'}}</span>
                    <img src="./images/right_icon.png" alt="">
                </div>
                <van-popup 
                v-model="CoachShow"
                position="bottom"
                >
                    <van-picker show-toolbar :columns="Coachlist" value-key="Sname" @confirm="CoachSelect" @cancel="CoachNo"/>
                </van-popup>
            </div>
            <div class="save">
                <button type="button" @click="submitData()">提交</button>
            </div>
        </div>
    </div>
    
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            minDate: new Date(1900,1,1),
            //时间弹出层
            dateShow:false,
            currentDate:new Date(),
            //来源弹出层
            sourceShow:false,
            //来源下拉框数据
            sourceData:null,
            //选中的来源
            source:null,
            //顾问弹出层
            adviserShow:false,
            //顾问确定数据
            adviser:null,
            //会籍顾问下拉数据
            stafflist:null,
            //姓名
            name:null,
            //电话
            Phone:null,
            //日期
            date:null,
            // //备注
            // Info:null,
            //性别
            Sex:1,
            //教练id
            Coach:null,
			//教练弹出层
			CoachShow:false,
            //教练下拉数据
            Coachlist:null,
            // minDate:new Date()-100000
        },
        created() {
            //获取路由参数
            let id = getUrlQueryString('id');
            this.id = id;
            if(id){
                //是编辑,获取用户详细信息
                axios.get(`mobileapi/boss/user.php?act=edit&uid=${id}`).then(res=>{
                    if(res.data.code == 200){
                        let user = res.data.data.user;
                        // this.sourceData = res.data.data.cfrom;
                        // this.stafflist = res.data.data.stafflist;
                        this.name = user.Username;
                        this.Phone = user.Phone;
                        this.Sex = user.Sex;
                        console.log(this.Sex);
                        this.date = user.Birth;
                        this.source = {
                            id:user.Userfrom,
                            Fromname:user.Userfrom_name
                        };
                        this.adviser = {
                            id:user.Sale,
                            Sname:user.Sale_name
                        };
						this.Coach = {
							id:user.Coach,
							Sname:user.Coach_name
						};
                        //this.Coach = user.Coach;
                    }else{
                        vant.Toast(res.data.msg);
                    }
                }).catch(err=>{
                    vant.Toast('请求发送失败，请重试');
                })
            }
            //是新增
            //获取下拉框数据
            axios.get('mobileapi/boss/user.php?act=add').then(res=>{
                if(res.data.code == 200){
                    //获取成功
                    this.sourceData = res.data.data.cfrom;
                    this.stafflist = res.data.data.stafflist;
					this.Coachlist = res.data.data.coachlist;
                }else{  
                    //获取失败
                    vant.Toast('数据获取失败，请重试')
                }   
            }).catch(err=>{
                vant.Toast('请求发送失败，请重试')
            })
        },
        methods: {
            //选择性别
            getRadioVal(event){
                this.Sex = event.target.value;
            },
            //展示时间弹出层
            showDate(){
                this.dateShow = true;
                
            },
            //选中日期
            dateSelect(val){
                //转化时间
                let time = changeDate(val);
                //关闭弹出层
                this.date = time;
                this.dateShow = false;
                console.log(time);
            },
            //日期取消
            dateNo(){
                //关闭弹出层
                this.dateShow = false;
            },
            //点击来源显示弹出层
            showSource(){
                this.sourceShow = true;
            },
            //确认来源
            sourceSelect(val){
                console.log(val);
                this.source = val;
                this.sourceShow = false;
            },
            //取消来源
            sourceNo(){
                this.sourceShow = false;
            },
            //顾问确认
            adviserSelect(val){
                this.adviser = val;
                this.adviserShow = false;
            },
            //顾问取消
            adviserNo(){
                this.adviserShow = false;
            },
            //顾问确认
            CoachSelect(val){
                this.Coach = val;
                this.CoachShow = false;
            },
            //顾问取消
            CoachNo(){
                this.CoachShow = false;
            },
          //提交按钮
            submitData(){
                let Username = this.name;
                let Phone = this.Phone;
                let Birth = this.date;
                let Sex = this.Sex;
                //验证
                if(Username == null || Username == ''){
                    vant.Toast('请输入姓名');
                    return false;
                }
                if(Phone == null || Phone == ''){
                    vant.Toast('请输入手机号');
                    return false;
                }
                if(Sex === null || Sex == ''){
                    vant.Toast('请选择性别');
                    return false;
                }
                if(Birth == null || Birth == ''){
                    vant.Toast('请选择出生年月');
                    return false;
                }
                if(this.source == null || this.source == ''){
                    vant.Toast('请选择来源');
                    return false;
                }
                if(this.adviser == null || this.adviser == ''){
                    vant.Toast('请选择会籍顾问');
                    return false;
                }
				if(this.Coach == null || this.Coach == ''){
                    vant.Toast('请选择教练');
                    return false;
                }
                let Userfrom = this.source.id;
                let Sale = this.adviser.id;
                let from = null;
                if(this.id == null){
                    //添加
                    from = {
                        Username,
                        Phone,
                        Sex,
                        Birth,
                        Userfrom,
                        Sale,
						Coach:this.Coach
                    }
                }else{
                    from = {
                        Username,
                        Sex,
                        Birth,
                        Userfrom,
                        Sale,
                        uid:this.id,
                        Coach:this.Coach
                    }
                }
                let froms = JSON.stringify(from);
                let sign = md5(froms);
                let params = {
                    from:froms,
                    sign
                }
                //判断是修改还是添加
                if(this.id == null){
                    //添加
                    axios.post('mobileapi/boss/user.php?act=haddgo',Qs.stringify(params)).then(res=>{
                        if(res.data.code == 200){
                            vant.Toast('添加成功');
                            setTimeout(()=>{
                                // history.go(-1);
                                location.href = "my_member.html";
                            },1500);
                        }else{
                            vant.Toast(res.data.msg);
                        }
                    }).catch(err=>{
                        vant.Toast('请求发送失败，请重试');
                    })
                }else{
                    //修改
                    axios.post('mobileapi/boss/user.php?act=heditgo',Qs.stringify(params)).then(res=>{
                        if(res.data.code == 200){
                            vant.Toast('修改成功');
                            return false;
                            setTimeout(()=>{
                                location.href = "memberDetails.html?id="+this.id;
                            },1500);
                        }else{
                            vant.Toast(res.data.msg);
                        }
                    }).catch(err=>{
                        vant.Toast('请求发送失败，请重试');
                    })
                }
            }
        },
    })
</script>
</html>